import React, {Fragment} from 'react';
import {Badge, Button, Switch, Tag, message, Menu, Dropdown, Icon, Divider, Modal} from 'antd';
import {hashHistory, Link} from 'react-router';
import ajax from '../utils/ajax';
import stockForm from '../components/GoodsList/SimpleStockForm';
import Utils from "../utils";
import Constants from "../utils/Constants";

module.exports = [
  {
    key: 'id',
    title: 'ID',
    dataType: 'int',
    primary: true,
    showType: 'normal',
    showInTable: true,
    showInForm: false,
    disabled: true,
    width: 80,
    render: (text, record) => text
  },
  {
    key: 'thumb',
    title: '缩略图',
    dataType: 'varchar',
    showType: 'image',
    width: 100,
  },
  {
    key: 'name',
    title: '商品名称',
    width: 150,
    dataType: 'varchar',
    render: (text, record) => {
      const tags = record.$$rawData.tags;
      const tagsElements = [];
      for (let item in tags) {
        tagsElements.push(
          <Tag key={tags[item].id} color={tags[item].color}>{tags[item].title}</Tag>
        );
      }

      return (
        <Fragment>
          <p>{text}</p>
          {tagsElements}
        </Fragment>
      );
    }
  },
  {
    key: 'goodsNo',
    title: '商品编号',
    dataType: 'varchar',
    width: 90
  },
  {
    key: 'categoryId',
    title: '商品分类',
    dataType: 'varchar',
    showType: 'select',
    width: 90,
    options: [],
    render(text, record) {
      if (!text) {
        return '';
      }
      return text.replace(/[├└]─ /, '');
    }
    //options: [{key: 1, value: "傻逼"}, {key: 15, value: "你妹啊"}]
  },
  {
    key: 'sellingPrice',
    title: '销售价格',
    width: 120,
    dataType: 'int',
    render: (text) => Utils.formatMoney(text / 100),
    sorter: (a, b) => a.sellingPrice - b.sellingPrice
  },
  {
    key: 'marketPrice',
    title: '市场价格',
    width: 120,
    dataType: 'int',
    render: (text) => Utils.formatMoney(text / 100),
    sorter: (a, b) => a.marketPrice - b.marketPrice
  },
  {
    key: 'stock',
    title: '库存',
    width: 90,
    showInForm: false,
    render: (text, row) => row.$$rawData.isInfiniteStock ? '无限' : text,
    sorter: (a, b) => a.stock - b.stock
  },
  {
    key: 'tagsId',
    title: '商品标签',
    dataType: 'varchar',
    showType: 'select',
    options: [],
    showInTable: false
  },
  {
    key: 'goodsSkus',
    title: 'SKU',
    showInTable: false,
    showInForm: false
  },
  {
    key: 'examineStatus',
    title: '审核状态',
    width: 110,
    dataType: 'int',
    showInForm: false,
    showType: 'select',
    options: Utils.map2kvArr(Constants.GOODS_EXAMINE_MAP),
    render: (text, record) => {
      const status = record.$$rawData.examineStatus;
      return (
        <div>
          <Badge status={status === 1 ? "success" : (status === 0 ? "default" : "error")}
                 text={text}/>
        </div>
      );
    }
  },
  {
    key: 'status',
    title: '上/下架',
    dataType: 'int',
    width: 90,
    showInForm: false,
    render: (text, record) => {
      if (record.$$rawData.examineStatus !== Constants.GOODS_EXAMINE_NORMAL) {
        return '';
      }

      return (
        <Switch checkedChildren="" onChange={(value) => {
          const status = value ? 0 : 1;
          ajax.post('/goods/changeStatus', {goodsId: record.id, status: status}).then((res) => {
            if (res.code === 0) {
              record.status = status;
              record.$$rawData.status = status;
              message.success('操作成功');

            } else {
              message.error(res.message);
            }
          }).catch(() => {
            message.error('操作失败');
          });
        }} unCheckedChildren="" defaultChecked={text === 0}/>
      );
    }
  },
  {
    key: 'singleRecordActions',
    title: '操作',  // 列名
    actions: [
      {
        name: <Button size="small" icon="database">入库</Button>,
        type: 'component',
        component: stockForm
      },
    ]
  },
  {
    key: 'other',
    title: '',
    width: 90,
    showInForm: false,
    render: (text, record) => {
      const SubMenu = Menu.SubMenu;

      // const showConfirm = () => {
      //   return confirm({
      //     title: '确定删除该商品吗？',
      //     okText: 'Yes',
      //     okType: 'danger',
      //     cancelText: 'No',
      //     onOk() {
      //       ajax.post('/goods/recycle', {goodsId: record.id}).then((res) => {
      //         if (res.code === 0) {
      //           message.success('操作成功');
      //           setTimeout(() => {
      //             hashHistory.push('/goodsManage/JumpPage');
      //           }, 1000);
      //         } else {
      //           message.error(res.message);
      //         }
      //       }).catch(() => {
      //         message.error('操作失败');
      //       });
      //     },
      //   });
      // };

      const callFun = (item) => {
        ajax.post('/goods/changeExamine', {examine: item.key, goodsId: record.id}).then((res) => {
          if (res.code === 0) {
            message.success('操作成功');
            setTimeout(() => {
              window.location.reload();
            }, 1000);
          } else {
            message.error(res.message);
          }
        }).catch(() => {
          message.error('操作失败');
        });
      };

      let examine;
      if (record.$$rawData.examineStatus === Constants.GOODS_EXAMINE_WAIT) {
        examine = (
          <SubMenu title="审核商品">
            <Menu.Item key="1" onClick={callFun}>通过</Menu.Item>
            <Menu.Item key="2" onClick={callFun}>拒绝</Menu.Item>
          </SubMenu>
        );
      }

      const menu = (
        <Menu>
          {examine}
          <Menu.Item>
            <Link to={`/goodsManage/GoodsDetails/` + record.id}>{'查看信息'}</Link>
          </Menu.Item>
        </Menu>
      );

      const MoreBtn = () => (
        <Dropdown overlay={menu}>
          <a>
            更多 <Icon type="down"/>
          </a>
        </Dropdown>
      );
      return (
        <div>
          <MoreBtn/>
        </div>
      );
    }
  },
];